var tabs = {
	"desktop": {
		name: "我的桌面",
		iframe: basePath+"desktop.do",
		key:"desktop"
	},
	"purchaseRequest": {
		name: "采购申请",
		iframe: basePath+"purchaseRequest/listPurchaseRequest.do",
		key:"purchaseRequest"
	},
	"purchaseAgent": {
		name: "待采购物资",
		iframe: basePath+"purchaseAgent/listPurchaseAgent.do",
		key:"purchaseAgent"
	},
	"purchaseOrder": {
		name: "采购订单",
		iframe: basePath+"purchaseOrder/listPurchaseOrder.do",
		key:"purchaseOrder"
	},
	"returnRequest": {
		name: "退货申请",
		iframe: basePath+"returnRequest/listReturnRequest.do",
		key:"returnRequest"
	},
	"generalStorage": {
		name: "普通入库",
		iframe: basePath+"generalStorage/selectByWhere.do",
		key:"generalStorage"
	},
	"pickingReturn": {
		name: "领料归还",
		iframe: basePath+"pickingReturnContrlooer/selectByWhere.do",
		key:"pickingReturn"
	},
	"returnAgent": {
		name: "待退货物资",
		iframe: basePath+"returnAgent/listReturnAgent.do",
		key:"returnAgent"
	},
	"returnOrder": {
		name: "退货出库",
		iframe: basePath+"returnOrder/listReturnOrder.do",
		key:"returnOrder"
	},
	"getLeaveStorage": {
		name: "领用出库",
		iframe: basePath+"picking/listPicking.do",
		key:"getLeaveStorage"
	},
	"storageManage": {
		name: "库存查询",
		iframe: basePath+"inventory/inventoryQuery.do",
		key:"storageManage"
	},
	"storageDistribute": {
		name: "库存调配",
		iframe: basePath+"storageDistribute/selectByWhere.do",
		key:"storageDistribute"
	},
	"storageLoss": {
		name: "库存报损",
		iframe: basePath+"inventoryLoss/selectByWhere.do",
		key:"storageLoss"
	},
	"storageInventory": {
		name: "库存盘点",
		iframe: basePath+"inventoryLiquidation/selectByWhere.do",
		key:"storageInventory"
	},
	"inventoryWarning": {
		name: "库存预警",
		iframe: basePath+"inventory/inventoryIteminfo.do",
		key:"inventoryWarning"
	},
	"expiryDateWarning": {
		name: "有效期预警",
		iframe: basePath+"inventory/validityWarning.do",
		key:"expiryDateWarning"
	},
	"shopWarning": {
		name: "店仓预警",
		iframe: basePath+"iteminfo/inventoryIteminfo.do",
		key:"shopWarning"
	},
	"purchaseStorage": {
		name: "采购入库",
		iframe: basePath+"warehouseOrder/selectByWhere.do",
		key:"purchaseStorage"
	},
	"purchaseReport": {
		name: "物品采购统计表",
		iframe: basePath+"statistics/apsselectByWhere.do",
		key:"purchaseReport"
	},
	"storageReport": {
		name: "物品入库统计表",
		iframe: basePath+"statistics/selectByWhere.do",
		key:"storageReport"
	},
	"inventoryReport": {
		name: "物品盘点统计表",
		iframe: basePath+"inventoryLiquidation/selectInventoryLiquidationStatisticsList.do",
		key:"inventoryReport"
	},
	"lossReport": {
		name: "物品报损统计表",
		iframe: basePath+"inventoryLoss/selectInventoryLossStatisticsList.do",
		key:"lossReport"
	},
	"pickingReport": {
		name: "物品领用统计表",
		iframe: basePath+"statistics/pickingSelectByWhere.do",
		key:"pickingReport"
	},
	"returnReport": {
		name: "物品退货统计表",
		iframe: basePath+"statistics/returnorderSelectByWhere.do",
		key:"returnReport"
	},
	"purchaseRequestApprove": {
		name: "采购申请审批",
		iframe: basePath+"audit/listPurchaseRequestAudit.do",
		key:"purchaseRequestApprove"
	},
	"purchaseOrderApprove": {
		name: "采购订单审批",
		iframe: basePath+"audit/listPurchaseOrderAudit.do",
		key:"purchaseOrderApprove"
	},
	"returnOrderApprove": {
		name: "退货申请审批",
		iframe: basePath+"audit/listReturnRequestAudit.do",
		key:"returnOrderApprove"
	},
	"mySubmittedApprove": {
		name: "我提交的审批",
		iframe: basePath+"auditRecord/mySubmittedAudit.do",
		key:"mySubmittedApprove"
	},
	"approveByMe": {
		name: "我审批的审批",
		iframe: basePath+"auditRecord/approveByMe.do",
		key:"approveByMe"
	},
	"goodsManage": {
		name: "物品管理",
		iframe: basePath+"iteminfo/listItem.do",
		key:"goodsManage"
	},
	"goodsType": {
		name: "物品分类",
		iframe: basePath+"category/listCategory.do",
		key:"goodsType"
	},
	"supplierManage": {
		name: "供应商管理",
		iframe: basePath+"supplier/listSupplier.do",
		key:"supplierManage"
	},
	"supplyAgreement": {
		name: "供货协议",
		iframe: basePath+"supplyagreement/paginglist.do",
		key:"supplyAgreement"
	},
	"shopManage": {
		name: "店仓管理",
		iframe: basePath+"shop/listShop.do",
		key:"shopManage"
	},
	"warehouseManage": {
		name: "仓库管理",
		iframe: basePath+"warehouse/listWarehouse.do",
		key:"warehouseManage"
	},
	"inventoryLocationManage": {
		name: "库位管理",
		iframe: basePath+"inventorylocation/listInventoryLocation.do",
		key:"inventoryLocationManage"
	},
	"userManage": {
		name: "用户管理",
		iframe: basePath+"user/selectByWhere.do",
		key:"userManage"
	},
	"roleManage": {
		name: "角色管理",
		iframe: basePath+"role/selectByWhere.do",
		key:"roleManage"
	},
	"jurisdictionManage": {
		name: "权限管理",
		iframe: basePath+"Jurisdiction/selectByWhere.do",
		key:"jurisdictionManage"
	},
	"auditFlow": {
		name: "审批流程",
		iframe: basePath+"auditFlow/listAllFlow.do",
		key:"auditFlow"
	}
};

var Tab = {
	init: function() {
		var defaultTabList = ["desktop"];  // 默认打开会员管理
		var _this = this;

		this.tabList = [];
		this.addEvent();

		$.each(defaultTabList, function(index,value) {
			_this.toggleTab(value);
		});
	},
	toggleTab: function(id) {
		var index = $.inArray(id, this.tabList);

		if (index !== -1) {
			$(".tab_item").eq(index).click();
		} else {
			if (this.tabList.length === 0) {
				$(".tab").show();
			}

			this.createTab(id);
		}
	},
	createTab: function(id) {
		this.tabList.push(id);

		var $tabItem = this.createTabItem(id);
		var $content = this.createTabContent(id);
			
		$content.find("iframe").height($(".page").height() - 65 - $(".tab_head").height());

		$(".tab_head").append($tabItem);
		$(".tab_body").append($content);

		$tabItem.click();
		$("iframe").height($(".page").height() - 65-$(".tab_head").height());
	},
	createTabItem: function(id) {
		var name = tabs[id].name;
		var key = tabs[id].key;
		return $('<a class="tab_item" id="iframe_tab_'+key+'">' + name + '&nbsp;<i class="tab_close">&times;</i></a>');
	},
	createTabContent: function(id) {
		var url = tabs[id].iframe;
		var key = tabs[id].key;
		return $('<div class="tab_content"><iframe src="' + url + '" frameborder="0" class="iframe" id="iframe_'+key+'"></iframe></div>');
	},
	addEvent: function() {
		var _this = this;

		$("body").on("click", ".tab_item", function() {
			var index = $(this).index();
			$(this).addClass("active").siblings(".active").removeClass("active");
			$(".tab_content").eq(index).addClass("active").siblings(".active").removeClass("active");
			var name=$(this).attr("id").substring(11);
			var tags=$(".nav_item");
			$.each(tags, function(index,obj) {
				var key=$(obj).attr("data-detail");
				if(key==name){
					var $item = $(obj).parent();
					var $menu = $(obj).parent().parent().parent();
					$(".asidenav_list .active").removeClass("active");
					$(".asidenav_list .open").removeClass("open");
					$item.addClass("active");
					$menu.addClass("open");
				}
			});
		});

		$("body").on("click", ".tab_close", function() {
			var index = $(this).parent(".tab_item").index();

			_this.tabList.splice(index,1);

			$(this).parent(".tab_item").remove();
			$(".tab_content").eq(index).remove();

			if (_this.tabList.length === 0) {
				$(".tab").hide();
			} else {
				$(".tab_item").eq(index - 1).click();
			}

			return false;
		});
	}
}

$(function() {
	Tab.init();
	$("body").on("click", ".nav_item", function() {
		var name = $(this).data("detail");
		Tab.toggleTab(name);
	});
});



function openTabFrame(name,url){
	Tab.toggleTab(name);
	if(url){
		var iframe=$(".tab_content.active iframe")[0];
		iframe.contentWindow.location.href=url;
	}
}